package com.hospital.controller;

import com.hospital.domain.DTO.ReservationDTO;
import com.hospital.domain.pojo.Reservation;
import com.hospital.domain.utils.Result;
import com.hospital.service.ReservationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/reservation")
@Tag(name = "预约记录接口", description = "无")
public class ReservationController {
    private final ReservationService reservationService;

    @Operation(summary = "预约挂号")
    @PostMapping("/action")
    public Result reservation(@RequestBody ReservationDTO reservationDTO){
        try {
            if (reservationDTO == null) {
                return Result.fail("请求体为空");
            }
            Integer pId = reservationDTO.getPId();
            Integer dId = reservationDTO.getDId();
            Integer depId = reservationDTO.getDepId();
            String date = reservationDTO.getDate();
            String time = reservationDTO.getTime();
            String info = reservationDTO.getInfo();
            if (pId == null || dId == null || date == null || time == null || date.trim().isEmpty() || time.trim().isEmpty()) {
                return Result.fail("参数不完整，pId/dId/date/time 不能为空");
            }
            return reservationService.reservation(pId, dId, depId, date, time, info);
        } catch (Exception e) {
            return Result.fail("预约失败：" + e.getMessage());
        }
    }

    @Operation(summary = "取消预约")
    @DeleteMapping("/delete/{rId}")
    public Result deleteReservation(@PathVariable("rId") Integer rId){
        return reservationService.deleteReservation(rId);
    }

    @Operation(summary = "病人查询预约情况")
    @GetMapping("/select/{pId}")
    public Result queryByPId(@PathVariable("pId") Integer pId){
        if(pId == null){
            return Result.fail("服务器异常，请联系管理员");
        }
        List<Reservation> reservations = reservationService.lambdaQuery()
                .eq(Reservation::getPId, pId).list();
        return Result.ok(reservations);
    }

    @Operation(summary = "医生点击排班表查询预约情况")
    @GetMapping("/query/{sId}")
    public Result queryBySId(@PathVariable("sId") Integer sId){
        if(sId == null){
            return Result.fail("服务器异常，请联系管理员");
        }
        List<Reservation> reservations = reservationService.lambdaQuery()
                .eq(Reservation::getSId, sId).list();
        return Result.ok(reservations);
    }

    @Operation(summary = "查看预约详情")
    @GetMapping("/info/{rId}")
    public Result getReservationById(@PathVariable("rId") Integer rId){
        if(rId == null){
            return Result.fail("服务器异常，请联系管理员");
        }
        return Result.ok(reservationService.getById(rId));
    }

    @Operation(summary = "获取所有预约信息（管理员用）")
    @GetMapping("/queryAll")
    public Result queryAllReservations(){
        return reservationService.queryAllReservations();
    }
}
